package com.vil.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.vil.common.ServerResponse;
import com.vil.entity.Role;
import com.vil.mapper.RoleMapper;
import com.vil.service.RoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
@Service
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleMapper mapper;
    @Override
    public ServerResponse insertSelective(Role record) {
        record.setCreate_time(new Date());
        int i = mapper.insertSelective(record);
        if (i>0){
            return ServerResponse.buildSuccessMsg("新增成功");
        }
        return ServerResponse.buildErrorMsg("新增失败");
    }

    @Override
    public ServerResponse deleteByPrimaryKey(Long id) {
        int i = mapper.deleteByPrimaryKey(id);
        if (i>0){
            return ServerResponse.buildSuccessMsg("删除成功");
        }
        return ServerResponse.buildErrorMsg("删除失败");
    }

    @Override
    public ServerResponse updateByPrimaryKeySelective(Role record) {
        int i = mapper.updateByPrimaryKeySelective(record);
        if (i>0){
            return ServerResponse.buildSuccessMsg("修改成功");
        }
        return ServerResponse.buildErrorMsg("修改失败");
    }

    @Override
    public ServerResponse getListByManyConditions(Integer pageIndex, Integer pageSize, Role recode) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Role> list = mapper.getListByManyConditions(recode);
        PageInfo pageInfo = new PageInfo(list);
        return ServerResponse.buildSuccessData(pageInfo);
    }

    @Override
    public ServerResponse getById(Long id) {
        Role role = mapper.selectByPrimaryKey(id);
        if (role!=null){
            return ServerResponse.buildSuccessData(role);
        }
        return ServerResponse.buildErrorMsg("不存在该对象");
    }

    @Override
    public ServerResponse deleteMany(Long[] ids) {
        int i = mapper.deleteMany(ids);
        if (i>0){
            return ServerResponse.buildSuccessMsg("批量删除成功");
        }
        return ServerResponse.buildErrorMsg("批量删除失败");
    }

    @Override
    public ServerResponse isExist(String param) {
        int exist = mapper.isExist(param);
        if (exist>0){
            return ServerResponse.buildErrorMsg("该参数值已存在，请重新输入");
        }
        return ServerResponse.buildSuccessMsg("该参数值可以使用");
    }

}
